package net.zdreamer.geracomium.ui.login;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.geracomium.R;

import net.zdreamer.geracomium.application.BaseApplication;
import net.zdreamer.geracomium.myviews.CustomProgressDialog;
import net.zdreamer.geracomium.ui.MainActivity;
import net.zdreamer.geracomium.utils.account.LoginService;
import net.zdreamer.geracomium.utils.tools_utils.URLTools;

public class LogInActivity extends Activity implements OnClickListener {
    private TextView forget_password;
    private EditText edit_uername, edit_password;
    private ImageView login, resgist, loginlogo;
    private String username, password;
private Dialog lodingdialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);

        login = (ImageView) findViewById(R.id.btn_login);
        resgist = (ImageView) findViewById(R.id.regist);
        edit_uername = (EditText) findViewById(R.id.username);
        edit_password = (EditText) findViewById(R.id.password);
        forget_password = (TextView) findViewById(R.id.login_forget_password);
        login.setOnClickListener(this);
        resgist.setOnClickListener(this);
        forget_password.setOnClickListener(this);
        forget_password.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        lodingdialog=CustomProgressDialog.createLoadingDialog(LogInActivity.this, "登录中，请稍后");
    }

    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.arg1) {
                case 0:
                    lodingdialog.dismiss();
                    Toast.makeText(LogInActivity.this, "输入错误，请核实后重试^v^", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    lodingdialog.dismiss();
                    Toast.makeText(LogInActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    Intent main = new Intent(LogInActivity.this, MainActivity.class);
                    BaseApplication.get().setToken("ZDREAMER-TOKEN "+msg.obj.toString());
                    startActivity(main);
                    LogInActivity.this.finish();
                    break;
                default:
                    break;
            }

            // System.out.println(msg.objg);
        }

        ;
    };

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_login:
//                if (edit_uername.getText().toString().trim().equals("")) {
//                    Toast.makeText(LogInActivity.this, "请输入用户名", Toast.LENGTH_LONG)
//                            .show();
//                } else if (edit_password.getText().toString().trim().equals("")) {
//                    Toast.makeText(LogInActivity.this, "请输入密码", Toast.LENGTH_LONG)
//                            .show();
//                } else {
//                    username = edit_uername.getText().toString().trim();
//                    password = edit_password.getText().toString().trim();
//                }
//                username="android";
//                password="123456";
//                BaseApplication.get().setUserId(username);
//                LoginService.loginServicce(username, password, handler, URLTools.LogInUrl);
//                lodingdialog.show();
//                Intent main = new Intent(LogInActivity.this, MainActivity.class);
//                startActivity(main);

                Intent main = new Intent(LogInActivity.this, MainActivity.class);
                startActivity(main);
                LogInActivity.this.finish();
                break;
            case R.id.regist:
                Intent Regist = new Intent();
                Regist.setClass(LogInActivity.this, RegistActivity.class);
                startActivity(Regist);
                break;

            case R.id.login_forget_password:
                Intent for_password = new Intent(LogInActivity.this,
                        ForgetPasswordActivity.class);
                startActivity(for_password);
                break;
            default:
                break;
        }
    }

    private long firstTime = 0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    Toast.makeText(LogInActivity.this, "再按一次退出程序",
                            Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;
                    return true;
                } else { // 如果间隔小于2S则推出
                    int currentVersion = android.os.Build.VERSION.SDK_INT;
                    if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                        Intent startMain = new Intent(Intent.ACTION_MAIN);
                        startMain.addCategory(Intent.CATEGORY_HOME);
                        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(startMain);
                        System.exit(0);
                    } else {// android2.1
                        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                        am.restartPackage(getPackageName());
                    }

                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

}
